<script setup lang="ts">
import { useAddressStore } from '@/stores'
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { Address } from '@/types/address'
// import { getAddressAPI } from '@/services/address'
// import type { AddressType } from '@/types/address'
// const addressList = ref<AddressType[]>([])
// const getAddress = async () => {
//   const res = await getAddressAPI()
//   console.log(res.result)
// }
// onShow(() => {
//   getAddress()
// })
const emit = defineEmits<{
  (e: 'close'): void
  (e: 'changeRegion', newRegion: string): void
}>()
const gotoAddress = () => {
  uni.navigateTo({ url: '/pagesMember/address-form/address-form' })
}
const address = ref<Address[]>([])
address.value = useAddressStore().addressList
onShow(() => {
  address.value = useAddressStore().addressList
})
const confirmAddress = () => {
  emit('changeRegion', useAddressStore().isDefaultAddress!)
  emit('close')
}
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <image @tap="emit('close')" class="close" src="@/static/images/remove.png"></image>
    <!-- 标题 -->
    <view class="title">配送至</view>
    <!-- 内容 -->
    <view class="content" v-if="address.length > 0">
      <view class="item" v-for="(item, index) in address" :key="index">
        <view class="user">{{ item.receiver + ' ' + item.contact }}</view>
        <view class="address">{{ item.region }}</view>
        <text
          class="icon"
          :class="{ 'icon-checked': item.isDefault === 1, 'icon-ring': item.isDefault === 0 }"
        ></text>
      </view>
    </view>
    <view class="empty" v-else>
      <image class="blank-image" src="@/static/images/blank.png"></image>
      <text class="blank-text">暂无收货地址</text>
    </view>
    <view class="footer">
      <view v-if="address.length > 0" class="button primary" @tap="confirmAddress">确定</view>
      <view v-else class="button primary" @tap="gotoAddress">新建地址</view>
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 30rpx 30rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 25rpx;
  top: 30rpx;
  width: 50rpx;
  height: 50rpx;
}

.content {
  min-height: 300rpx;
  max-height: 540rpx;
  overflow: auto;
  padding: 20rpx;
  .item {
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    background-image: url(@/static/images/locate.png);
    position: relative;
  }
  .icon {
    color: #999;
    font-size: 40rpx;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 0;
  }
  .icon-checked {
    color: #00bf9c;
  }
  .icon-ring {
    color: #444;
  }
  .user {
    font-size: 28rpx;
    color: #444;
    font-weight: 500;
  }
  .address {
    font-size: 26rpx;
    color: #666;
  }
}

.empty {
  height: 300rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .blank-image {
    width: 150rpx;
    height: 150rpx;
  }
  .blank-text {
    color: gray;
    font-size: 24rpx;
  }
}
.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #00bf9c;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
